/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import event.JobConfirmEvent;
import event.JobSendableEvent;
import net.sf.appia.core.AppiaEventException;
import net.sf.appia.core.Direction;
import net.sf.appia.core.Event;
import net.sf.appia.core.Layer;
import net.sf.appia.core.Session;
import net.sf.appia.core.events.channel.ChannelInit;
import net.sf.appia.protocols.common.SendableNotDeliveredEvent;
import net.sf.appia.protocols.common.RegisterSocketEvent;
import util.Constants;
import util.Debug;

/**
 *
 * @author Welington
 */
public class JobSession extends Session {

    private Class current = JobSession.class;
    private Debug debug = Debug.getInstance();

    public JobSession(Layer layer) {
        super(layer);
    }

    @Override
    public void handle(Event event) {
        if (event instanceof ChannelInit) {
            handleChannelInit((ChannelInit) event);
        } else if (event instanceof RegisterSocketEvent) {
            handleRegisterSocket((RegisterSocketEvent) event);
        } else if (event instanceof JobSendableEvent) {
            if (event.getDir() == Direction.DOWN) {
                handleSendableEvent((JobSendableEvent) event);
            } else {
                handleDeliverySendableEvent((JobSendableEvent) event);
            }
        } else if (event instanceof SendableNotDeliveredEvent) {
            handleSendableNotDelivered((SendableNotDeliveredEvent) event);
        }
    }

    private void handleChannelInit(ChannelInit event) {
        try {
            event.go();
        } catch (AppiaEventException e) {
            debug.print(Debug.ERROR, current, e.getLocalizedMessage());
        }
    }

    private void handleRegisterSocket(RegisterSocketEvent event) {
        try {
            event.go();
        } catch (AppiaEventException ex) {
            debug.print(Debug.ERROR, current, ex.getLocalizedMessage());
        }
    }

    private void handleSendableEvent(JobSendableEvent event) {
        try {
            debug.print(Debug.LOG, current, "Delivering message to network layer {" + Constants.extractContent(event.getMessage()) + "}");
            event.go();

            JobConfirmEvent confirm = new JobConfirmEvent();
            confirm.setMessage(Constants.extractContent(event.getMessage()));
            confirm.setId(event.getId());
            confirm.setChannel(event.getChannel());
            confirm.setDir(Direction.UP);
            confirm.setSourceSession(this);
            try {
                confirm.init();
                confirm.go();
            } catch (AppiaEventException ex) {
                debug.print(Debug.ERROR, current, ex.getLocalizedMessage());
            }
        } catch (AppiaEventException e) {
            debug.print(Debug.ERROR, current, e.getLocalizedMessage());
        }
    }

    private void handleDeliverySendableEvent(JobSendableEvent event) {
        debug.print(Debug.INFO, current, "Message received from Network Layer. Sending to upper layer...");

        try {
            event.go();
        } catch (AppiaEventException ex) {
            debug.print(Debug.ERROR, current, ex.getLocalizedMessage());
        }
    }

    private void handleSendableNotDelivered(SendableNotDeliveredEvent sendableNotDeliveredEvent) {
        debug.print(Debug.ERROR, current, "Not yet implemented - handleSendableNotDelivered");
    }
}
